<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of welcome
 *
 * @author Obi
 */
class Keranjang extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('cart');
        $this->load->library('format_rupiah');
        $this->m_produk_det = new M_produk_det();
        $this->m_tarip_kirim = new M_tarif_kirim();

        $this->mkota = new M_kota();
    }

    function index() {

   
        $data['menu'] = array(
        );

        $data['title'] = "Keranjang Pemesanan";
        $data['list_keranjang'] = $this->cart->contents();

        $data['isi'] = 'isi_keranjang';
        $this->load->view('template_client', $data);
    }

    function add_item() {

        $id_produk_det = $this->input->post('produk_dett');
        $bnyk = $this->input->post('bnyk');

        $dt = $this->m_produk_det->produk_det($id_produk_det);
        $item = array(
            'id' => $id_produk_det,
            'qty' => $bnyk,
            'price' => $dt->harga,
            'name' => $dt->nama_produk,
            'options' => array(
                'Size' => $dt->size_produk,
                'Color' => $dt->warna,
                'disc' => $dt->disk,
                'id_produk' => $dt->id_produk,
                'berat' => $dt->berat,
                'image' => $dt->image1
            ),
        );

        $this->cart->insert($item);

        $data['list_keranjang'] = $this->cart->contents();


        $data['menu'] = array(
        );
        $data['title'] = " Keranjang Pemesanan";
        $data['isi'] = 'isi_keranjang';
        $this->load->view('template_client', $data);
    }

    function delete_item($id = NULL) {
        $data = array(
            'rowid' => $id,
            'qty' => 0
        );

        $this->cart->update($data);

        $this->index();
    }

    function total_biaya($id_kota = NULL, $id_mitra = NULL) {

        $data_biaya = $this->m_tarip_kirim->where("id_kota = $id_kota and id_mitra = $id_mitra")->get();
        $tujuan = $this->mkota->where("id = $id_kota")->get();
        $nama_kota = $tujuan->nama_kota;

        $biaya_kirim = $data_biaya->harga_kirim;
        $totberat = 0;
        $sub_total = 0;
        $grand = 0;
        $n = 0;
        $keranjang = $this->cart->contents();
        foreach ($keranjang as $dta) {
            $berat = $dta['options']['berat'];

            $harga = $dta['price'];
            $bnyk = $dta['qty'];
            $diskon = $dta['options']['disc'];

            $totberat = ($berat * $bnyk) + $totberat;


            $sub = ($harga * $bnyk) - ($harga * $bnyk) * $diskon / 100;
            $sub_total = $sub + $sub_total;
            $n++;
        }

        $total_biaya_kirim = ceil($totberat) * $biaya_kirim;

        $grand = $total_biaya_kirim + $sub_total;


        echo "<td> <p> $n Item Produk <br/> " . $this->format_rupiah->rupiah($sub_total) . " </p> </td>";
        echo "<td> + </td>";
        echo "<td> <p> Tujuan Pengiriman Ke $nama_kota   <br/>" . $this->format_rupiah->rupiah($total_biaya_kirim) . " <p> </td>";
        echo "<td> = </td>";
        echo "<td> <p>Jumlah Biaya Pemesanan <br/> " . $this->format_rupiah->rupiah($grand) . " </p> </td>";
    }

    function res_total_biaya() {

        $totberat = 0;
        $sub_total = 0;
        $grand = 0;
        $n = 0;
        $keranjang = $this->cart->contents();
        foreach ($keranjang as $dta) {
            $berat = $dta['options']['berat'];

            $harga = $dta['price'];
            $bnyk = $dta['qty'];
            $diskon = $dta['options']['disc'];

            $totberat = ($berat * $bnyk) + $totberat;


            $sub = ($harga * $bnyk) - ($harga * $bnyk) * $diskon / 100;
            $sub_total = $sub + $sub_total;
            $n++;
        }


        echo "<td> <p> $n Item Produk <br/> " . $this->format_rupiah->rupiah($sub_total) . " </p> </td>";
        echo "<td> + </td>";
        echo "<td> <p> -  <br/> Rp. 0 <p> </td>";
        echo "<td> = </td>";
        echo "<td> <p>Jumlah Biaya Pemesanan <br/> " . $this->format_rupiah->rupiah($sub_total) . " </p> </td>";
    }

}

?>
